package com.servlet;

import com.alibaba.fastjson.JSONObject;
import com.pojo.Export_info;
import com.pojo.Medicine_info;
import com.service.Export_infoService;
import com.service.impl.Export_infoServiceImpl;
import com.service.impl.Medicine_infoServiceImpl;
import com.util.PageUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Export_infoServlet", value = "/Export_infoServlet")
public class Export_infoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String tag = request.getParameter("tag");
        int pageIndex = request.getParameter("pageIndex") == null ? 1 : Integer.parseInt(request.getParameter("pageIndex"));
        int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request.getParameter("pageSize"));
        String search = request.getParameter("search") == null ? "" : request.getParameter("search");
        Export_infoService exportInfoService = new Export_infoServiceImpl();
        PrintWriter writer = response.getWriter();
        if ("edit".equals(tag)) {
            int exportNo = Integer.parseInt(request.getParameter("exportNo"));
            Export_info export = exportInfoService.findExportById(exportNo);
            PageUtil<Medicine_info> medicineInfoPageUtil = new Medicine_infoServiceImpl().pagequery(pageIndex, pageSize, null);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("export", export);
            jsonObject.put("medicines", medicineInfoPageUtil);
            String exportJsonStr = JSON.toJSONString(jsonObject);
            System.out.println("出库编辑页面数据"+exportJsonStr);
            writer.write(exportJsonStr);
            writer.close();
        } else if ("del".equals(tag)) {
            int exportNo = Integer.parseInt(request.getParameter("exportNo"));
            int delFlag = exportInfoService.delExport(exportNo);
            writer.print(delFlag > 0);
            writer.close();
        } else {
            PageUtil<Export_info> pageExportList = exportInfoService.findPageExportList(pageIndex, pageSize, search);
            String pageExportListJsonStr = JSON.toJSONString(pageExportList);
            System.out.println(pageExportListJsonStr);
            writer.write(pageExportListJsonStr);
            writer.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String tag = request.getParameter("tag");
        int medicineId = Integer.parseInt(request.getParameter("medicineId"));
        String customer = request.getParameter("customer");
        String exportDate = request.getParameter("exportDate");
        int quantity = Integer.parseInt(request.getParameter("quantity"));
        double unitPrice = Double.parseDouble(request.getParameter("unitPrice"));
        double totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
        int status = Integer.parseInt(request.getParameter("status"));
        Export_info exportInfo = new Export_info(0, new Medicine_infoServiceImpl().queryMedicineInfoById(medicineId), customer, exportDate, quantity, unitPrice, totalPrice, status);
        Export_infoService exportInfoService = new Export_infoServiceImpl();
        PrintWriter writer = response.getWriter();
        if ("addSave".equals(tag)) {
            int addFlag = exportInfoService.addExport(exportInfo);
            writer.print(addFlag > 0);
            writer.close();
        } else if ("editSave".equals(tag)) {
            int exportNo = Integer.parseInt(request.getParameter("exportNo"));
            exportInfo.setExportNo(exportNo);
            int editFlag = exportInfoService.updateExport(exportInfo);
            writer.print(editFlag > 0);
            writer.close();
        }
    }
}
